package org.example.excel;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFDrawing;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 
/**
 * @className: ExcelUtil
 * @description: [描述说明该类的功能]
 * @author: [姓名]
 * @version: [v1.0]
 * @createTime: [2023/9/20 15:50]
 * @updateUser: [姓名]
 * @updateTime: [2023/9/20 15:50]
 * @updateRemark: [描述说明本次修改内容]
 */
public class ExcelUtil {
    private List<ExcelSheet> sheetList=new ArrayList<>();
    private String rootPath = "";
    private String imageTargetPath = "";
    private Workbook workbook = null;
 
    public ExcelUtil(String filePath,String imageTargetPath ){
        this.rootPath=filePath;
        this.imageTargetPath=imageTargetPath;
        readExcel(filePath);
    }
    /**
     * excel插入图片
     * @param book poi book对象
     * @param drawingPatriarch 用于图片插入Represents a SpreadsheetML drawing
     * @param rowIndex 图片插入的单元格第几行
     * @param colIndex 图片插入的单元格第几列
     * @param localPicPath 本地图片路径
     */
    public static void insertExcelPic(XSSFWorkbook book, XSSFDrawing drawingPatriarch, int rowIndex, int colIndex, String localPicPath) throws IOException {
        // 获取图片后缀格式
        String fileSuffix = localPicPath.substring(localPicPath.lastIndexOf(".") + 1);
        fileSuffix = fileSuffix.toLowerCase();

        // 将图片写入到字节数组输出流中
        BufferedImage bufferImg;
        ByteArrayOutputStream picByteOut = new ByteArrayOutputStream();
        bufferImg = ImageIO.read(new File(localPicPath));
        ImageIO.write(bufferImg, fileSuffix, picByteOut);

        // 将图片字节数组输出流写入到excel中
        XSSFClientAnchor anchor = new XSSFClientAnchor(12, 3, 0, 0,
                (short) colIndex, rowIndex, (short) colIndex + 1, rowIndex + 1);
        anchor.setAnchorType(ClientAnchor.AnchorType.MOVE_AND_RESIZE);
        drawingPatriarch.createPicture(anchor, book.addPicture(picByteOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
        picByteOut.close();
    }
    private void readExcel(String path) {
        try {
            this.rootPath = path;
            // 获取文件输入流
            InputStream inputStream = new FileInputStream(path);
            // 定义一个org.apache.poi.ss.usermodel.Workbook的变量
 
            // 截取路径名 . 后面的后缀名，判断是xls还是xlsx
            String fileType = path.substring(path.lastIndexOf(".") + 1);
 
            if (fileType.equals("xls")) {
                workbook = new HSSFWorkbook(inputStream);
            } else if (fileType.equals("xlsx")) {
                workbook = new XSSFWorkbook(inputStream);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    public ExcelSheet getSheet(int index,Integer titleRow){
        Sheet sheet = workbook.getSheetAt(index);
        return new ExcelSheet(sheet,titleRow,rootPath,imageTargetPath);
    }
}